package cn.tedu.dao;/**
 * @Author 金帮嘉诚 @Date 2021-07-17 星期六 15:05
 */
/*本类用于测试*/

import cn.tedu.pojo.Car;
import cn.tedu.utils.JdbcUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 知识点
 * <p>
 * crud
 */
@Service
public class CarDaoImpl implements CarDao{
    /**
     * 插入
     */
    public static String insert(Car car) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            connection = JdbcUtils.getCon("cgb2105");
            String sql = "insert into spb_car values (null,?,'类1',?,?);";
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, car.getName());
            preparedStatement.setString(2, car.getColor());
            preparedStatement.setDouble(3, car.getPrice());

            if (preparedStatement.executeUpdate() == 0) {
                return "插入失败！";
            } else {
                return "插入成功！";
            }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return "插入失败！请检查JdbcUtil类中的Driver类导入代码";
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return "插入失败！请检查insert()中代码！";
        } finally {
            JdbcUtils.closeRPC(resultSet, preparedStatement, connection);
        }

    }

    /**
     * 查询
     */
    public List<Car> get(String carType) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        List<Car> carList = new ArrayList<>();

        try {
            connection = JdbcUtils.getCon("cgb2105");
            String sql = "select * from spb_car where type = ?;";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, carType);

            resultSet = preparedStatement.executeQuery();

            carList = getList(resultSet);

            return carList;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("插入失败！请检查JdbcUtil类中的Driver类导入代码");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            System.out.println("插入失败！请检查insert()中代码！");
        } finally {
            JdbcUtils.closeRPC(resultSet, preparedStatement, connection);
        }

        return carList;
    }

    /**
     * 根据结果集对象返回ArrayList<Car>
     */
    private static List<Car> getList(ResultSet resultSet) throws SQLException {
        Integer id;
        String name;
        String type;
        String color;
        Double price;

        //有@SpringBootApplication注解，不能使用这个类
//        Car car = (Car) new ClassPathXmlApplicationContext("spring-config.xml").getBean("car");

        //千万不要把car写在循环外，因为carList里面存储的是多个引用对象，所以要分别创建car再存入carList
//        Car car = new Car();
        List<Car> carList = new ArrayList<>();
//        System.out.println(carList);//[]

        while (resultSet.next()) {
            Car car=new Car();
            id = resultSet.getInt(1);
            name = resultSet.getString(2);
            type = resultSet.getString(3);
            color = resultSet.getString(4);
            price = resultSet.getDouble(5);
            car.setId(id).setName(name).setType(type).setColor(color).setPrice(price);
            //Car(id=12, name=n12, type=t12, color=c12, price=10000.0)
            //Car(id=13, name=n13, type=t12, color=c13, price=200000.0)
            //Car(id=14, name=n14, type=t12, color=c14, price=3000000.0)
//            System.out.println(car);
            carList.add(car);
            //[Car(id=12, name=n12, type=t12, color=c12, price=10000.0)]
            //[Car(id=13, name=n13, type=t12, color=c13, price=200000.0), Car(id=13, name=n13, type=t12, color=c13, price=200000.0)]
            //[Car(id=14, name=n14, type=t12, color=c14, price=3000000.0), Car(id=14, name=n14, type=t12, color=c14, price=3000000.0), Car(id=14, name=n14, type=t12, color=c14, price=3000000.0)]
//            System.out.println(carList);
        }
        //[Car(id=14, name=n14, type=t12, color=c14, price=3000000.0), Car(id=14, name=n14, type=t12, color=c14, price=3000000.0), Car(id=14, name=n14, type=t12, color=c14, price=3000000.0)]
//        System.out.println(carList);
        return carList;
    }


}
